package com.daxia.bumall.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.bumall.common.DesignerOrderType;
import com.daxia.bumall.common.RecommonStatus;
import com.daxia.bumall.dto.CityDTO;
import com.daxia.bumall.dto.CommunityDTO;
import com.daxia.bumall.dto.CompanyDTO;
import com.daxia.bumall.dto.DesignDTO;
import com.daxia.bumall.dto.DesignerDTO;
import com.daxia.bumall.dto.OrderDesignerDTO;
import com.daxia.bumall.model.City;
import com.daxia.bumall.model.Designer;
import com.daxia.bumall.support.Page;

@Controller
@RequestMapping("designer")
public class DesignerController extends BaseController { 
	
	@RequestMapping("order")
	public String index(Map<String, Object> map, String from,HttpServletRequest request) throws Exception {
		String agent=request.getHeader("user-agent");
		System.out.println(agent+"================================");
		map.put("provinces", provinceService.findAll());
//		map.put("houseStyles", houseStyleService.findAll());
		map.put("from", from);
		if(isMobileDevice(agent))
			return "designer/designer_order_mobile";
		else
			return "designer/designer_order";
	}
	
	private boolean isMobileDevice(String requestAgent){
		String[] deviceArray=new String[]{"android","mac os","windows phone"};
		if(requestAgent==null)
			return false;
		requestAgent=requestAgent.toLowerCase();
		for(String device : deviceArray){
			if(requestAgent.indexOf(device)>0){
				return true;
			}
		}
		return false;
	}
	/**
	 * 一键预约
	 * @param name
	 * @param mobile
	 * @param provinceId
	 * @param cityId
	 * @param communityId
	 * @param designerId
	 * @param from
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping("orderDesigner")
    public String orderDesigner(String name, String mobile, Long provinceId, Long cityId, Long communityId,Long designerId,String from,Long companyId) throws Exception {
		if ("姓名".equals(name)) {
			name = "";
		}
		if ("手机号".equals(mobile)) {
			mobile = "";
		}
		assertTrue(StringUtils.isNotBlank(name), "请填写姓名");
		assertTrue(StringUtils.isNotBlank(mobile), "请填写手机号码");
		assertTrue(provinceId != null, "请选择省份");
		assertTrue(cityId != null, "请选择城市");
		
		OrderDesignerDTO dto = new OrderDesignerDTO();
		if(designerId != null) {
			Designer designer = new Designer();
			designer.setId(designerId);
			dto.setDesigner(designer);
		}
		dto.setUser(getCurrentUserDTO());
		dto.setArea(null);
		if (cityId != null) {
			dto.setCity(new City());
			dto.getCity().setId(cityId);
		}
		if (communityId != null) {
			CommunityDTO communityDTO = communityService.load(communityId);
			dto.setCommunity(communityDTO);
			dto.setCommunityName(communityDTO.getName());
		}
		if(companyId!=null){
			CompanyDTO company=companyService.load(companyId);
			dto.setCompany(company);		
		}
		dto.setCreateTime(new Date());
		dto.setHouseStyle(null);
		dto.setHouseType(null);
		dto.setOrderTime(null);
		dto.setOrderName(name);
		dto.setOrderMobile(mobile);
		dto.setFrom(from);
		orderDesignerService.create(dto);
        return okJson();
    }
	
	@RequestMapping("index")
	public String designerIndex(Map<String, Object> map, Long cityId,String goodAtStyles,Integer orderType,Long designerId) throws Exception {
		prepareCategories(map);
		map.put("cityId", cityId);
		map.put("goodAtStyles", goodAtStyles);
		map.put("orderType", orderType);
		
		List<CityDTO> citys=cityService.findCitysByDesigner();
		map.put("citys", citys);
		map.put("houseStyles", houseStyleService.findAll());
		
		DesignerDTO query=new DesignerDTO();
		if(cityId!=null){
			City city=new City();
			city.setId(cityId);
			query.setCity(city);
		}
		if(null!=goodAtStyles&&!"".equals(goodAtStyles)){
			goodAtStyles=goodAtStyles.replace("风格", "");
			query.setGoodAtStyles(goodAtStyles);
		}
		if(orderType!=null&&orderType!=0){
			query.setOrderType(DesignerOrderType.getByValue(orderType));
		}
		query.setRecommend(RecommonStatus.Recommened.getValue());
//		page.setPageSize(10);
		List<DesignerDTO> designers=designerService.find(query, null);
		map.put("designers", designers);
		
		if(designers==null||designers.size()==0){
			designerId=null;
		}
		
		if(null==designerId){  //未选择设计师
			if(designers!=null&&designers.size()>0){
				designerId=designers.get(0).getId();
			}
		}
		
		if(null!=designerId){  //查询设计方案
			DesignDTO qy=new DesignDTO();
			Designer des=new Designer();
			des.setId(designerId);
			qy.setDesigner(des);
			DesignDTO design=designService.findOne(qy);
			map.put("design", design);
		}
		map.put("designerId", designerId);
		return "designer/designer_index";
	}
	
	@RequestMapping("detail")
	public String detail(Map<String, Object> map, Long designerId , Page page) throws Exception {
		prepareCategories(map);
		if(designerId==null||designerId==0){
			return onException(new Exception("请选择设计师~!"));
		}
		DesignDTO query=new DesignDTO();
		DesignerDTO query2=new DesignerDTO();
		Designer designer = new Designer();
		designer.setId(designerId);
		query.setDesigner(designer);
		query2.setId(designerId);
		page.setPageSize(9);
		List<DesignDTO> designs= designService.find(query, page);
		List<DesignerDTO> designers=designerService.find(query2, page);
		if(designers!=null&&designers.size()>0){
			designer=designers.get(0);
			map.put("designer", designer);
		}
		map.put("designerId", designerId);
		map.put("designs", designs);
		map.put("page", page);
		map.put("provinces", provinceService.findAll());
		return "designer/designer_detail";
	}
}	
